﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using qiupeng.Public;
using System.IO;

using Ths.Framework;
using Ths.Components.Common;
using Ths.Components.Dictionary;
using Ths.Components.Dictionary.Entity;
using SafetyRecordData.Data.DAL;
public partial class DistressManage_NotHurtList : Base
{
    Db List = new Db();
    public static string CountsLabel, AllPageLabel, CurrentlyPageLabel, SqlStrStart, SqlStrMid, SqlStr, SqlStrStartCount, SqlStrCount;
    DAHurtDistress dal = new DAHurtDistress();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (this.Session["username"] == null)
        {
            this.Response.Write("<script language=javascript>alert('登陆超时！');window.parent.location = '../main_d.aspx'</script>");
            return;

        }

        if (!Page.IsPostBack)
        {
            BindAttribute();
            Bindquanxian();
            DataBindToGridview("order by id desc");

            if (GetRequestStr("islook") == "1")
            {
                this.Img_Add.Visible = false;
                this.Img_Delete.Visible = false;
                this.Img_Search.Visible = false;
                this.Img_Update.Visible = false;
            }
        }
    }

    #region 初始化
    /// <summary>
    /// 权限绑定
    /// </summary>
    public void Bindquanxian()
    {
        List.QuanXianVis(Img_Add, "bbbb1a", Session["perstr"].ToString());
        List.QuanXianVis(Img_Show, "bbbb1b", Session["perstr"].ToString());
        List.QuanXianVis(Img_Update, "bbbb1c", Session["perstr"].ToString());
        List.QuanXianVis(Img_Delete, "bbbb1d", Session["perstr"].ToString());
        List.QuanXianVis(Img_Search, "bbbb1e", Session["perstr"].ToString());

        List.QuanXianVis(Img_Submmit, "bbbb1n", Session["perstr"].ToString());
        List.QuanXianVis(Img_ShenHe, "bbbb1k", Session["perstr"].ToString());
        List.QuanXianVis(Img_TuiHui, "bbbb1m", Session["perstr"].ToString());
    }

    public void BindAttribute()
    {
        Img_Update.Attributes["onclick"] = "javascript:return updatecheck();";
        Img_Add.Attributes["onclick"] = "javascript:return showwait();";
        Img_Delete.Attributes["onclick"] = "javascript:return delcheck();";
        Img_Show.Attributes["onclick"] = "javascript:return updatecheck();";


        btnFirst.Attributes["onclick"] = "javascript:return showwait();";
        btnPrev.Attributes["onclick"] = "javascript:return showwait();";
        btnNext.Attributes["onclick"] = "javascript:return showwait();";
        btnLast.Attributes["onclick"] = "javascript:return showwait();";
        btnReturn.Attributes["onclick"] = "javascript:return chkyema();";
    }
    #endregion

    public void DataBindToGridview(string Sqlsort)
    {
        SqlStrStart = "select * from NotHurtDistress where 1=1";
        SqlStrStartCount = "select count(id) from NotHurtDistress where 1=1";


        SqlStrMid = string.Empty;
        SqlStrMid = CreateMidSql();
        if (Request.QueryString["bmid"] != null)
        {
            if (Request.QueryString["bmid"] != "999" && Request.QueryString["bmid"] != "" && Request.QueryString["bmid"] != "undefined")
            {
                string bmID = new DAHurtDistress().GetDetailDepartMent(Request.QueryString["bmid"].ToString());
                SqlStrMid += " and  companyid in ('" + bmID.Replace(",", "','") + "')";
            }
            if (Request.QueryString["starttime"] != "" && Request.QueryString["starttime"] != "undefined" && Request.QueryString["starttime"] != null)
            {
                SqlStrMid += " and  happentime>='" + Request.QueryString["starttime"].ToString().Trim() + " 00:00:00" + "'";
            }

            if (Request.QueryString["endtime"] != "" && Request.QueryString["endtime"] != "undefined" && Request.QueryString["endtime"] != null)
            {
                SqlStrMid += " and  happentime<='" + Request.QueryString["endtime"].ToString().Trim() + " 23:59:59" + "'";
            }
            if (Request.QueryString["parentbmid"] != null)
            {
                if (Request.QueryString["bmid"] != "")
                {
                    string parentbmid = new DAHurtDistress().GetDetailDepartMent(Request.QueryString["parentbmid"].ToString());
                    SqlStrMid += " and  companyid in ('" + parentbmid.Replace(",", "','") + "')";
                }

            }
            SqlStrStart += " and status='55'  ";
            SqlStrStartCount += " and status='55'";
            this.Img_ShenHe.Visible = false;
            this.Img_TuiHui.Visible = false;
        }
        else
        {
            switch (Session["ResponseRemark"].ToString())
            {
                case "普通":
                    SqlStrStart += " and status='0'  ";
                    SqlStrStartCount += " and status='0' ";
                    break;
                case "区队":
                    SqlStrStart += " and (isnull(status,'0')='0' or isnull(status,'0')='12') and  companyid in ('" + Session["bmqxa"].ToString().Replace(",", "','") + "') ";
                    SqlStrStartCount += " and (isnull(status,'0')='0' or isnull(status,'0')='12') and  companyid in ('" + Session["bmqxa"].ToString().Replace(",", "','") + "') ";
                    break;
                case "矿级":
                    SqlStrStart += " and status='1' and  companyid in ('" + Session["bmqxa"].ToString().Replace(",", "','") + "')  ";
                    SqlStrStartCount += " and status='1'  and  companyid in ('" + Session["bmqxa"].ToString().Replace(",", "','") + "') ";
                    break;
                case "局级":
                    SqlStrStart += " and status='55'  ";
                    SqlStrStartCount += " and status='55'";
                    break;
            }

        }


        SqlStr = SqlStrStart + SqlStrMid;//查询
        SqlStrCount = SqlStrStartCount + SqlStrMid;//查询个数



        string SQL_GetList_xs = "" + SqlStr + " " + Sqlsort + "";
        GridView1.DataSource = List.GetGrid_Pages_not(SQL_GetList_xs);
        GridView1.DataBind();

        string SQL_GetCount = SqlStrCount;
        CountsLabel = "" + List.GetCount(SQL_GetCount) + "";

        AllPageLabel = Convert.ToString(GridView1.PageCount);
        CurrentlyPageLabel = Convert.ToString(((int)GridView1.PageIndex + 1));

        btnFirst.CommandName = "1";
        btnPrev.CommandName = (GridView1.PageIndex == 0 ? "1" : GridView1.PageIndex.ToString());

        btnNext.CommandName = (GridView1.PageCount == 1 ? GridView1.PageCount.ToString() : (GridView1.PageIndex + 2).ToString());
        btnLast.CommandName = GridView1.PageCount.ToString();
    }





    private string CheckCbxDel()
    {
        string str = "0";
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            GridViewRow row = GridView1.Rows[i];
            CheckBox Chk = (CheckBox)row.FindControl("CheckSelect");
            Label LabVis = (Label)row.FindControl("LabVisible");
            if (Chk.Checked == true)
            {
                if (str == "")
                {
                    str = LabVis.Text.ToString();
                }
                else
                {
                    str = str + "," + LabVis.Text.ToString();
                }
            }
        }
        return str;
    }


    private string CheckCbxNameDel()
    {
        string str = string.Empty;
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            GridViewRow row = GridView1.Rows[i];
            CheckBox Chk = (CheckBox)row.FindControl("CheckSelect");
            Label LabVis = (Label)row.FindControl("LabNameVisible");
            if (Chk.Checked == true)
            {
                if (str == "")
                {
                    str = LabVis.Text.ToString();
                }
                else
                {
                    str = str + "," + LabVis.Text.ToString();
                }
            }
        }
        return str;
    }

    private string CheckCbxUpdate()
    {
        string str = string.Empty;
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            GridViewRow row = GridView1.Rows[i];
            CheckBox Chk = (CheckBox)row.FindControl("CheckSelect");
            Label LabVis = (Label)row.FindControl("LabVisible");
            if (Chk.Checked == true)
            {
                if (str == "")
                {
                    str = LabVis.Text.ToString();
                }
                else
                {
                    str = str + "," + LabVis.Text.ToString();
                }
            }
        }
        return str;
    }

    public string CreateMidSql()
    {
        string MidSql = string.Empty;
        if (Request.QueryString["key"] != null)
        {
            if (Request.QueryString["Name"] != null)
            {
                MidSql = MidSql + " and Company like '%" + List.GetFormatStr(Server.UrlDecode(Request.QueryString["Name"])) + "%'";
            }
        }
        return MidSql;

    }

    protected void Img_Delete_Click(object sender, ImageClickEventArgs e)
    {
        string CheckStr = CheckCbxDel();

        string SqlStr = "  Delete from NotHurtDistress  where appid in ('" + CheckStr.Replace(",", "','") + "')";
        List.ExeSql(SqlStr);

        List.InsertLog("删除非伤亡事故[" + CheckCbxNameDel() + "]", "非伤亡事故管理");

        DataBindToGridview("order by id desc");
    }

    protected void Img_Update_Click(object sender, ImageClickEventArgs e)
    {

        Response.Redirect("NotHurt_update.aspx?appid=" + CheckCbxUpdate() + "");

    }

    protected void Img_Add_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("NotHurt_update.aspx?IsNew=1");
    }



    protected void Img_Show_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("NotHurt_update.aspx?appid=" + CheckCbxUpdate() + "&view=1&islook="+GetRequestStr("islook")+"");
    }

    public string GetStatus(string strCode)
    {
        switch (strCode)
        {
            case "":
                return "暂存";
                break;
            case "0":
                return "暂存";
                break;
            case "1":
                return "提交";
                break;
            case "12":
                return "退回";
                break;
            case "55":
                return "审核通过";
                break;
            default:
                break;
        }
        return "";
    }

    #region GridView基本信息
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        DataControlRowType itemType = e.Row.RowType;
        if (e.Row.RowType != DataControlRowType.Header)
        {
            e.Row.Attributes["onmouseover"] = "javascript:setMouseOverColor(this);";
            e.Row.Attributes["onmouseout"] = "javascript:setMouseOutColor(this);";


            Label lblLeval = (Label)e.Row.FindControl("lblLevel");
            if (lblLeval != null)
            {
                lblLeval.Text = dal.GetBasicDataName(lblLeval.Text.Trim().Replace("&nbsp;", ""), "13");
            }

            Label lblType = (Label)e.Row.FindControl("lblType");
            if (lblType != null)
            {
                lblType.Text = dal.GetBasicDataName(lblType.Text.Trim().Replace("&nbsp;", ""), "15");
            }
            try
            {
                e.Row.Cells[6].Text = GetStatus(e.Row.Cells[6].Text.Replace("&nbsp;", ""));
            }
            catch (Exception er) { }

        }
    }

    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        string sql = "";

        if (ViewState["SortDirection"] == null || ViewState["SortDirection"].ToString().CompareTo("") == 0)
        {
            ViewState["SortDirection"] = " desc";
        }
        else
            ViewState["SortDirection"] = "";

        sql = " order by " + e.SortExpression + ViewState["SortDirection"];


        DataBindToGridview(sql);
    }


    #endregion

    #region 分页需要
    public override void VerifyRenderingInServerForm(Control control)
    { }

    protected void PagerButtonClick(object sender, EventArgs e)
    {
        try
        {
            GridView1.PageIndex = Convert.ToInt32(((LinkButton)sender).CommandName) - 1;
            DataBindToGridview("order by id desc");
        }
        catch
        {
            Response.Write("<script language='javascript'>alert('没有这么多页！');</script>");
            return;
        }
    }

    protected void btnReturn_Click1(object sender, EventArgs e)
    {
        try
        {
            if (GoPage.Text.Trim().ToString() == "")
            {
                Response.Write("<script language='javascript'>alert('页码不可以为空!');</script>");
            }
            else if (GoPage.Text.Trim().ToString() == "0" || Convert.ToInt32(GoPage.Text.Trim().ToString()) > GridView1.PageCount)
            {
                Response.Write("<script language='javascript'>alert('页码不是一个有效值!');</script>");
            }
            else if (GoPage.Text.Trim() != "")
            {
                int PageI = Int32.Parse(GoPage.Text.Trim()) - 1;
                if (PageI >= 0 && PageI < (GridView1.PageCount))
                {
                    GridView1.PageIndex = PageI;
                }
            }
            DataBindToGridview("order by id desc");
        }
        catch
        {
            DataBindToGridview("order by id desc");
            Response.Write("<script language='javascript'>alert('请输入有效的页码字符！');</script>");
        }
    }
    #endregion


    protected void Img_Submmit_Click(object sender, ImageClickEventArgs e)
    {
        string CheckStr = CheckCbxDel();

        string SqlStr = " update  NotHurtDistress set status='1'  where appid in ('" + CheckStr.Replace(",", "','") + "')";
        List.ExeSql(SqlStr);
        Alert("提交成功！", this.Img_Submmit);
        List.InsertLog("提交非伤亡事故[" + CheckCbxNameDel() + "]", "非伤亡事故管理");

        DataBindToGridview("order by id desc");
    }
    protected void Img_ShenHe_Click(object sender, ImageClickEventArgs e)
    {
        string CheckStr = CheckCbxDel();

        string SqlStr = " update  NotHurtDistress set status='55'  where appid in ('" + CheckStr.Replace(",", "','") + "')";
        List.ExeSql(SqlStr);
        Alert("审核成功！", this.Img_Submmit);
        List.InsertLog("审核非伤亡事故[" + CheckCbxNameDel() + "]", "非伤亡事故管理");

        DataBindToGridview("order by id desc");
    }
    protected void Img_TuiHui_Click(object sender, ImageClickEventArgs e)
    {
        string CheckStr = CheckCbxDel();

        string SqlStr = " update  NotHurtDistress set status='12'  where appid in ('" + CheckStr.Replace(",", "','") + "')";
        List.ExeSql(SqlStr);
        Alert("退回成功！", this.Img_Submmit);
        List.InsertLog("退回非伤亡事故[" + CheckCbxNameDel() + "]", "非伤亡事故管理");

        DataBindToGridview("order by id desc");
    }
}
